package com.haoge7777.weblog.web.controller;

import com.haoge7777.weblog.common.aspect.ApiOperationLog;
import com.haoge7777.weblog.common.domian.mapper.UserMapper;
import com.haoge7777.weblog.common.enums.ResponseCodeEnum;
import com.haoge7777.weblog.common.exception.BizException;
import com.haoge7777.weblog.common.utils.JsonUtil;
import com.haoge7777.weblog.common.utils.Response;
import com.haoge7777.weblog.web.model.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.BindResult;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.stream.Collectors;


@RestController
@Slf4j
@Api(tags = "首页模块")
public class TestController {


    //测试传入数据是否成功
    @PostMapping("/test")
    @ApiOperationLog(description = "测试接口")
    @ApiOperation(value = "测试传入数据是否成功")
    public User test1(@RequestBody User user){

        return user;
    }

    //测试传入数据是否具有参数校验功能
    @PostMapping("/test2")
    @ApiOperationLog(description = "测试接口")
    @ApiOperation(value = "测试传入数据是否具有参数校验功能")
    public ResponseEntity<String> test2(@RequestBody @Validated User user, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            String errorMsg = bindingResult.getFieldErrors()
                    .stream()
                    .map(FieldError::getDefaultMessage)
                    .collect(Collectors.joining("，"));

                    return ResponseEntity.badRequest().body(errorMsg);

        }

        return ResponseEntity.ok("参数没有任何问题");

    }

    //测试数据是否存在校验错误并显示错误相关信息的功能
    @PostMapping("/test3")
    @ApiOperationLog(description = "测试接口")
    @ApiOperation(value = "测试数据是否存在校验错误并显示错误相关信息的功能")
    public Response test3(@RequestBody @Validated User user,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            String errorMsg = bindingResult.getFieldErrors()
                    .stream()
                    .map(FieldError::getDefaultMessage)
                    .collect(Collectors.joining("，"));

            //返回错误数据
                return Response.fail(errorMsg);
        }

        //返回成功数据
        return Response.success();
    }

    //测试全局异常功能
    @PostMapping("/test4")
    @ApiOperationLog(description = "测试接口")
    @ApiOperation(value = "测试全局异常功能")
    public Response test4(@RequestBody @Validated User user,BindingResult bindingResult){
        // 手动抛异常，入参是前面定义好的异常码枚举，反参统一交给异常处理器搞定
        throw new BizException(ResponseCodeEnum.PRODUCT_NOT_FOUND);
    }

    //测试异常码枚举传入功能
    @PostMapping("/test5")
    @ApiOperationLog(description = "测试接口")
    @ApiOperation(value = "测试异常码枚举传入功能")
    public Response test5(@RequestBody @Validated User user,BindingResult bindingResult){
        //主动定义一个运行时异常，分母不能为零
        int i = 1/0;
        return Response.success();
    }

    //测试全局异常管理器
    @PostMapping("/test6")
    @ApiOperationLog(description = "测试接口")
    @ApiOperation(value = "测试全局异常管理器")
    public Response test6(@RequestBody @Validated User user){
        return Response.success();
    }

    //测试新日期API功能是否实现
    @PostMapping("/admin/test7")
    @ApiOperationLog(description = "测试接口")
    @ApiOperation(value = "测试接口")
    public Response test7(@RequestBody @Validated User user){
        //打印入参
        log.info(JsonUtil.toJsonString(user));

        //设置三种日期字段值
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateDate(LocalDate.now());
        user.setTime(LocalTime.now());

        return Response.success(user);
    }




}
